package com.example.recyclerviewpooldemo;

import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.Arrays;
import java.util.Random;

public class MainActivity extends AppCompatActivity {
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		ViewPager pager = (ViewPager) findViewById(R.id.pager);
		pager.setAdapter(new PageAdapter(getSupportFragmentManager()));
	}

	static class PageAdapter extends FragmentPagerAdapter {
		RecyclerView.RecycledViewPool mPool = new RecyclerView.RecycledViewPool() {
			@Override
			public RecyclerView.ViewHolder getRecycledView(int viewType) {
				RecyclerView.ViewHolder scrap = super.getRecycledView(viewType);
				Log.i("@@@", "get holder from pool: " + scrap);
				return scrap;
			}

			@Override
			public void putRecycledView(RecyclerView.ViewHolder scrap) {
				super.putRecycledView(scrap);
				Log.i("@@@", "put holder to pool: " + scrap);
			}

			@Override
			public String toString() {
				return "ViewPool in adapter@" + Integer.toHexString(hashCode());
			}
		};

		public PageAdapter(FragmentManager fm) {
			super(fm);
		}

		@Override
		public Fragment getItem(int i) {
			RecyclerViewFragment f = new RecyclerViewFragment();
			f.mPool = mPool;
			return f;
		}

		@Override
		public int getCount() {
			return 10;
		}

		@Override
		public CharSequence getPageTitle(int position) {
			return "pos:" + position;
		}
	}

	public static class RecyclerViewFragment extends Fragment{
		RecyclerView.RecycledViewPool mPool;
		@Nullable
		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
			RecyclerView view = new RecyclerView(inflater.getContext());
			LinearLayoutManager layout = new LinearLayoutManager(inflater.getContext());
			layout.setRecycleChildrenOnDetach(true);
			view.setLayoutManager(layout);
			if (mPool != null) {
				view.setRecycledViewPool(mPool);
				Log.i("@@@", "using view pool :" + mPool);
			}
			view.setAdapter(new DemoAdapter());
			view.setItemViewCacheSize(10);
			return view;
		}
	}

	static class DemoAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
		String[] mTexts;
		DemoAdapter(){
			int count = new Random().nextInt(60);
			mTexts = new String[count];
			fillTexts(count);
		}

		private void fillTexts(int count) {
			Random random = new Random();
			for (int i = 0; i < count; i++) {
				char[] buf = new char[random.nextInt(10)];
				Arrays.fill(buf, (char) (random.nextInt(26) + 65));
				mTexts[i] = new String(buf);
			}
		}
		static final int TEXT = 0;
		static final int TITLE = 1;

		@Override
		public int getItemViewType(int position) {
			if (position % 10 == 0) return TITLE;
			return TEXT;
		}

		@Override
		public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
			TextView view = new TextView(viewGroup.getContext());
			view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
			if (viewType == TITLE) {
				return new TitleTextViewHolder(view);
			} else {
				return new TextViewHolder(view);
			}
		}

		@Override
		public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
			TextView itemView = (TextView) viewHolder.itemView;
			if(viewHolder instanceof TitleTextViewHolder) {
				itemView.setText("~~~"+mTexts[position]+"~~~~");
			}else{
				itemView.setText(position+". "+mTexts[position]);
			}
		}

		@Override
		public int getItemCount() {
			return mTexts.length;
		}

		static class TextViewHolder extends RecyclerView.ViewHolder{

			public TextViewHolder(TextView itemView) {
				super(itemView);
				itemView.setTextAppearance(itemView.getContext(), R.style.TextAppearance_AppCompat_Medium);
			}
		}

		static class TitleTextViewHolder extends RecyclerView.ViewHolder{

			public TitleTextViewHolder(TextView itemView) {
				super(itemView);
				itemView.setTextAppearance(itemView.getContext(), R.style.TextAppearance_AppCompat_Headline);
				itemView.setGravity(Gravity.CENTER_HORIZONTAL);
			}
		}
	}
}
